package vip.xiaozhao.paotui.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaozhao.paotui.intf.entity.PaotuiWallet;
import vip.xiaozhao.paotui.intf.entity.PaotuiWalletAudit;
import vip.xiaozhao.paotui.intf.entity.PaotuiWalletWithdraw;
import vip.xiaozhao.paotui.intf.entity.PaotuiWalletWithdrawChannel;
import vip.xiaozhao.paotui.intf.mapper.PaotuiWalletAuditMapper;
import vip.xiaozhao.paotui.intf.mapper.PaotuiWalletMapper;
import vip.xiaozhao.paotui.intf.mapper.PaotuiWalletWithdrawChannelMapper;
import vip.xiaozhao.paotui.intf.mapper.PaotuiWalletWithdrawMapper;
import vip.xiaozhao.paotui.intf.service.PaotuiWalletWithdrawService;

import java.math.BigDecimal;

@Service
@Slf4j
public class PaotuiWalletWithdrawServiceImpl implements PaotuiWalletWithdrawService {

    @Autowired
    private PaotuiWalletMapper walletMapper;

    @Autowired
    private PaotuiWalletAuditMapper walletAuditMapper;

    @Autowired
    private PaotuiWalletWithdrawMapper walletWithdrawMapper;

    @Autowired
    private PaotuiWalletWithdrawChannelMapper walletWithdrawChannelMapper;

    @Override
    public PaotuiWalletWithdraw loadById(int id) {
        return walletWithdrawMapper.loadById(id);
    }

    @Override
    public int insert(PaotuiWalletWithdraw walletWithdraw) {
        return walletWithdrawMapper.insert(walletWithdraw);
    }

    @Override
    public int update(PaotuiWalletWithdraw walletWithdraw) {
        return walletWithdrawMapper.update(walletWithdraw);
    }

    @Override
    @Transactional
    public boolean withdraw(BigDecimal withdrawAmount, int userId) {
        // 1 钱包的钱扣除金额
        // 1.1 判断余额是否足够
        PaotuiWallet wallet = walletMapper.loadByUserId(userId);
        if (wallet == null){
            log.info("wallet == null");
            return false;
        }
        if (wallet.getAmount().compareTo(withdrawAmount) < 0) {
            return false;
        }

        // 1.2 扣除钱包金额
        wallet.setAmount(wallet.getAmount().subtract(withdrawAmount));
        walletMapper.update(wallet);
        // todo 2.调用支付宝接口

        // 3 记录插入钱包流水表
        PaotuiWalletAudit walletAudit = new PaotuiWalletAudit();
        walletAudit.setAmount(withdrawAmount);
        walletAudit.setUserId(userId);
        walletAudit.setWalletId(wallet.getId());
        walletAudit.setStatus(1);
        walletAudit.setDesc("提现");
        walletAudit.setUserIp("");
        walletAuditMapper.insert(walletAudit);

        // 4 记录插入钱包退款渠道表
        PaotuiWalletWithdrawChannel withdrawChannel = new PaotuiWalletWithdrawChannel();
        withdrawChannel.setUserId(userId);
        withdrawChannel.setChannelNo("");
        withdrawChannel.setRealName("");
        withdrawChannel.setMobile("");
        withdrawChannel.setStatus(1);
        walletWithdrawChannelMapper.insert(withdrawChannel);

        // 5 记录插入钱包退款表
        PaotuiWalletWithdraw walletWithdraw = new PaotuiWalletWithdraw();
        walletWithdraw.setUserId(userId);
        walletWithdraw.setAmount(withdrawAmount);
        walletWithdraw.setStatus(1);
        walletWithdraw.setChannelId(withdrawChannel.getId());
        walletWithdrawMapper.insert(walletWithdraw);

        return true;
    }
}
